package com.vicene.gmail.order.service.impl;

import com.rabbitmq.client.Channel;
import com.vicene.gmail.order.entity.OrderEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vicene.gmail.common.utils.PageUtils;
import com.vicene.gmail.common.utils.Query;

import com.vicene.gmail.order.dao.OrderItemDao;
import com.vicene.gmail.order.entity.OrderItemEntity;
import com.vicene.gmail.order.service.OrderItemService;
import org.springframework.transaction.annotation.Transactional;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 同一个队列接受不同类型消息的方法
     * @param orderEntity
     */
    @RabbitHandler
    public void recieveMessage(OrderEntity orderEntity, Message message , Channel channel) {
        System.out.println("接收到的消息。。" + orderEntity);

        //入channel生成的自增号
        long deliverTag = message.getMessageProperties().getDeliveryTag();
        try{
            if(deliverTag %2 == 0){
                channel.basicAck(deliverTag , false);
                System.out.println("签收："+deliverTag);
            }else{
                // 1 deliverTag  2 批量签收是否  3 拒绝签收后true 重新入queue消费  false 直接丢弃
                //channel.basicNack(deliverTag , false,false);
                //channel.basicReject(deliverTag , false);
                System.out.println("未签收："+deliverTag);

            }
        }catch (Exception e){

        }

    }


    @RabbitHandler
    public void recieveMessage2( OrderItemEntity orderItemEntity){
        System.out.println("接收到的消息。。" + orderItemEntity);

    }

}